----------------------------------------------------------
-- 宝箱
----------------------------------------------------------

local Msg = require("core.Msg")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemLogic = require("bag.ItemLogic")
local ItemDefine = require("bag.ItemDefine")
local Broadcast = require("broadcast.Broadcast")

-- 发送自选宝箱列表
function sendBoxList(human, list)	
	local msgRet = Msg.gc.GC_BOX_QUERY
	msgRet.item[0] = list and #list or 0
    for i = 1, msgRet.item[0] do
    	local itemID = list[i][1]
    	local itemCnt = list[i][2]
        local quality = list[i][3]
        Grid.makeItem(msgRet.item[i], itemID, itemCnt, nil, nil, nil, nil, quality)
    end 
    --Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 自选宝箱-选择道具
function getBoxItem(human, itemID, sIndex, cnt)
	local itemConfig = ItemDefine.getConfig(itemID)
	if not itemConfig then
		return Broadcast.sendErr(human, Lang.ITEM_USE_ERR_CONFIG)
	end

	local cmdstr = itemConfig.cmd and itemConfig.cmd[1]
	if cmdstr ~= "box" then return end

    -- 检测数量
    local haveCnt = BagLogic.getItemCnt(human, itemID)
    if haveCnt <= 0 or haveCnt < cnt or cnt <= 0 then
       return Broadcast.sendErr(human, Lang.ITEM_USE_ERR_CONFIG)
    end

	local itemList = itemConfig.cmd[2]
    if not itemList[sIndex] then return end

	local sItemID = itemList[sIndex][1]
	local sItemCnt = itemList[sIndex][2]
    local quality = itemList[sIndex][3]
	if ItemDefine.getConfig(sItemID) == nil then
		return Broadcast.sendErr(human, Lang.ITEM_USE_ERR_CONFIG)
	end

	BagLogic.delItem(human, itemID, cnt, "item_use")
    
    local itemList = {}
	itemList[1] = {sItemID, sItemCnt * cnt, quality}
	BagLogic.addItemList(human, itemList, "useBox")


	ItemLogic.sendItemUse(human, itemID)	
end
